package maldade.windowslivelogin;

import java.io.InputStream;

import maldade.windowslivelogin.xmlobjects.Contact;
import maldade.windowslivelogin.xmlobjects.Contacts;
import maldade.windowslivelogin.xmlobjects.Email;
import maldade.windowslivelogin.xmlobjects.Emails;
import maldade.windowslivelogin.xmlobjects.Location;
import maldade.windowslivelogin.xmlobjects.Locations;
import maldade.windowslivelogin.xmlobjects.Personal;
import maldade.windowslivelogin.xmlobjects.Phone;
import maldade.windowslivelogin.xmlobjects.Phones;
import maldade.windowslivelogin.xmlobjects.Professional;
import maldade.windowslivelogin.xmlobjects.Profiles;
import maldade.windowslivelogin.xmlobjects.URI;
import maldade.windowslivelogin.xmlobjects.URIs;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.XppDomDriver;

/**
 * Singleton Windows live parser, XML to object converter.
 * 
 * @author rnojiri
 */
public class WindowsLiveXmlParser {
	
	private static WindowsLiveXmlParser instance;
	
	private XStream xstream;

	/**
	 * Initializes the parser with the XML mapping.
	 */
	private WindowsLiveXmlParser()
	{
		xstream = new XStream(new XppDomDriver());

xstream.alias("Contact", Contact.class);
		
		xstream.alias("Contacts", Contacts.class);
		xstream.addImplicitCollection(Contacts.class, "Contacts");
		
		xstream.alias("Profiles", Profiles.class);
		xstream.alias("Personal", Personal.class);
		xstream.alias("Professional", Professional.class);
		
		xstream.alias("Email", Email.class);
		xstream.addImplicitCollection(Emails.class, "Emails");
		
		xstream.alias("Phone", Phone.class);
		
		xstream.alias("Phones", Phones.class);
		xstream.addImplicitCollection(Phones.class, "Phones");
		
		xstream.alias("Location", Location.class);
		
		xstream.alias("Locations", Locations.class);
		xstream.addImplicitCollection(Locations.class, "Locations");
		
		xstream.alias("URI", URI.class);
		
		xstream.alias("URIs", URIs.class);
		xstream.addImplicitCollection(URIs.class, "URIs");
	}
	
	/**
	 * Returns the instantiated instance of this object.
	 * 
	 * @return WindowsLiveXmlParser
	 */
	public static WindowsLiveXmlParser getInstance()
	{
		if(instance == null)
		{
			instance = new WindowsLiveXmlParser();
		}
		
		return instance;
	}
	
	/**
	 * Read the XML from an input stream.
	 * 
	 * @param inputStream
	 * @return Contacts
	 */
	public Contacts convertoXml2Object(InputStream inputStream) throws Exception
	{
		return (Contacts)xstream.fromXML(inputStream);
	}
}
